package org.zfes.snowier.cloud.zbss.widget.act.route.service.stu;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.exceptions.ServiceLogicalException;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.web.controller.BaseController;
import org.zfes.snowier.auth.shiro.util.SecurityUtil;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActSubject;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActSubjectJoin;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActSubjectJoinService;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActSubjectService;
@Controller("org.zfes.snowier.cloud.zbss.widget.act.route.service.stu.actSubjectJoinController") 
@RequestMapping("/active/service/stu/actSubjectJoin")
public class ActSubjectJoinController extends BaseController {

	
	@Autowired
	private IActSubjectJoinService actSubjectJoinService;
	@Autowired
	private IActSubjectService actSubjectService;
	
	//二维码进入
    @RequestMapping(value="/enterActSubjectQR", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody Object enterActSubject(Long subjectId){
    	
    	
    	ActSubjectJoin join=new ActSubjectJoin();
		    	join.setSubjectId(subjectId)
			    	.setJoinUserId(SecurityUtil.getUserId().get())
			    	.setJoinUserName(SecurityUtil.getUserName().get())
			    	.setJoinTime(ZDateUtil.newDate());
		actSubjectJoinService.enterActSubject(join);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
    @RequestMapping(value="/enterActSubjectPin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody Object enterActSubject(String  piNum){
    	
		ActSubject actSubject=actSubjectService.loadActSubjectByPinNum(piNum).orElseThrow(()->new ServiceLogicalException("未查询到开启的课堂数据"));
		//#0未进行， 1进行中，2已结束 -1删除#
		ZAssert.equals(actSubject.getStatus(), Byte.valueOf("1"), "当前课堂不是【进行中】状态，请提醒开启");
    	
    	ActSubjectJoin join=new ActSubjectJoin();
		    	join.setSubjectId(actSubject.getId())
			    	.setJoinUserId(SecurityUtil.getUserId().get())
			    	.setJoinUserName(SecurityUtil.getUserName().get())
			    	.setJoinTime(ZDateUtil.newDate());
		actSubjectJoinService.enterActSubject(join);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
    
    @RequestMapping(value="/leftActSubject", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody Object leftActSubject(Long subjectId){
    	
		actSubjectJoinService.leftActSubject(subjectId, SecurityUtil.getUserId().get());
		
    	return ajaxDoneSuccess("数据操作成功 ");
    }
    
}